package com.sduept.nwld.primaryequipment.rest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sduept.bigdata.fault.breaker.service.BreakerActionQueryManager;
import com.sduept.bigdata.fault.dto.BreakerActionInfoDTO;
import com.sduept.core.dao.SuperDAO;

import net.sf.json.JSONArray;
@RestController
@RequestMapping("/breakerRunSafe")
public class BreakerRunSafeEvaluateService {
	@Autowired
	private SuperDAO dao;
	@Autowired
	private BreakerActionQueryManager baqm;
	
	/**
	 * 获取熄弧时间概率分布
	 * 
	 * @return
	 *//*

	@GetMapping("/getArcTime/{option}")
	public String getArcTime(@PathVariable("option") String option) {
		
		String arcTime = "{'xihu_avg_time':[['0.6',0],['1.1',2],['1.6',0],['2.1',2],['2.6',5],['3.1',5],['3.6',20],['4.1',4],['4.6',23],['5.1',10],['6.1',0],['6.6',8],['7.1',0],['7.6',3],['8.1',0]],"
				+ "'pro_density':[ 4, 3, 3, 3, 3, 3, 4, 4, 3.332, 4.165, 4.998, 4.998, 5.831,"
				+ " 2.500, 4.166, 4.166, 3.332, 2.499, 3.332, 4.165, 4.165, 4.165, 4.165,"
				+ " 2.499, 3.332, 3.332, 3.332, 3.332, 4.000, 2.500, 3.333, 4.166, 3.333,"
				+ " 4.167, 6.666, 3.333, 3.333, 3.333, 2.198, 4.166, 4.167, 5.833, 3.333,"
				+ " 3.332, 4.998, 3.332, 4.998, 4.998, 4.165, 5.000, 2.000, 4.165, 3.334,"
				+ " 5.000, 4.167, 4.166, 3.332, 4.165, 3.332, 4.166, 5.831, 4.167, 4.165,"
				+ " 7.000, 4.165, 3.334, 5.833, 5.831, 4.998, 0.937, 3.333, 6.000, 5.000,"
				+ " 5.831, 4.166, 4.165, 4.998, 5.000, 5.833, 6.667, 5.833 ]"
				+ "}";
		return JSONObject.parse(arcTime).toString();
	}*/
	/**
	 * 获取断路器动作信息(动作时间分布)
	 * 
	 * @return
	 */

	@GetMapping("/getBreakerActInfos/{option}")
	public String getBreakerActInfos(@PathVariable("option") String option) {
		JSONObject jso = JSONObject.parseObject(option);
		List<BreakerActionInfoDTO> list = baqm.getBreakerActionInfoByOption(
				(String) jso.get("vol"),
				(String) jso.get("manufacturer"),
				(String) jso.get("type"), (String) jso.get("startDate"),
				(String) jso.get("endDate"));
		List<BreakerActionInfoDTO> result=new ArrayList<>();
		for(BreakerActionInfoDTO b:list){
			if(b.getDistance0MomentsTime()>0){
				result.add(b);
			}
		}
		return JSONArray.fromObject(result).toString();
	}
	/**
	 * 获取断路器动作信息，动作时间在minRange-maxRange之间
	 * @param option
	 * @param minRange
	 * @param maxRange
	 * @return
	 */
	@GetMapping("/getBreakerActDetailInfos/{option}/{minRange}/{maxRange}")
	public String getBreakerActDetailInfos(@PathVariable("option") String option,@PathVariable("minRange") String minRange,@PathVariable("maxRange") String maxRange) {
		JSONObject jso = JSONObject.parseObject(option);
		if(StringUtils.isNotEmpty(minRange)) {
			minRange = Integer.parseInt(minRange) * 1000+"";
		}
		if(StringUtils.isNotEmpty(maxRange)) {
			maxRange = Integer.parseInt(maxRange) * 1000+"";
		}
		List<BreakerActionInfoDTO> list = baqm.getBreakerActionInfoByCondition(
				(String) jso.get("manufacturer"),
				(String) jso.get("type"), (String) jso.get("startDate"),
				(String) jso.get("endDate"), maxRange,minRange);
		List<BreakerActionInfoDTO> result=new ArrayList<>();
		for(BreakerActionInfoDTO b:list){
			if(b.getDistance0MomentsTime()>0){
				result.add(b);
			}
		}
		return JSONArray.fromObject(result).toString();
	}
	/**
	 * 根据站id和断路器id，获得历年熄弧时间
	 * @param stationId,breakerId
	 * @return
	 */

	@GetMapping("/getArcTimesByBreaker/{stationId}/{breakerId}/{startDate}/{endDate}")
	public String getArcTimesByBreaker(@PathVariable("stationId") String stationId,@PathVariable("breakerId") String breakerId,@PathVariable("startDate") String startDate,@PathVariable("endDate") String endDate) {
		String startDateStr=startDate.equals("0")?null:startDate;
		String endDateStr=endDate.equals("0")?null:endDate;
		String[] station = { stationId };
		String[] breaker = { breakerId };
		List<BreakerActionInfoDTO> breakerActionInfoList=baqm.getBreakerActionInfoByStationAndBreaker(station,breaker,startDateStr,endDateStr);
		return JSONArray.fromObject(breakerActionInfoList).toString();
	}
	
	/**
	 * 获取变电站所有断路器历史动作信息（分为ABC三相）
	 * @param sid,startDate,endDate
	 * @return{name:[[x,y],[x,y]]}
	 */

	@GetMapping("/getBreakerActTimesByStationId/{startDate}/{endDate}/{sid}")
	public String getBreakerActTimesByStationId(@PathVariable("startDate") String startDate,@PathVariable("endDate") String endDate,@PathVariable("sid") String sid) {
		String[] stationId = { sid };
		String startTime=("null").equals(startDate)?null:startDate;
		String endTime=("null").equals(endDate)?null:endDate;
		return getBreakerActInfoByConditions(stationId,startTime,endTime,"act");
	}
	/**
	 * 获取变电站所有断路器历史动作信息（分为ABC三相）
	 * @param sid,startDate,endDate
	 * @return{name:[[x,y],[x,y]]}
	 */

	@GetMapping("/getBreakerArcTimesByStationId/{startDate}/{endDate}/{sid}")
	public String getBreakerArcTimesByStationId(@PathVariable("startDate") String startDate,@PathVariable("endDate") String endDate,@PathVariable("sid") String sid) {
		String[] stationId = { sid };
		String startTime=("null").equals(startDate)?null:startDate;
		String endTime=("null").equals(endDate)?null:endDate;
		return getBreakerActInfoByConditions(stationId,startTime,endTime,"arc");
	}
	private String getBreakerActInfoByConditions(String[] stationId,String startTime,String endTime,String actOrArc){
		List<BreakerActionInfoDTO> breakerActionInfos = baqm
				.getDesignatedStationBreakerActionInfo(stationId, startTime,endTime);
		
		Map<String,List<String[]>> map=new HashMap<>();
		for (BreakerActionInfoDTO b : breakerActionInfos) {
			if("arc".equals(actOrArc)){
				if (b.getXhTimeInterval()>0&&"跳闸".equals(b.getActionType())) {
					String name=b.getBreakerName()+"-"+b.getPhase();
					String[] s={b.getFaultTime(),String.valueOf((float)b.getXhTimeInterval()/1000)};
					if(map.get(name)==null){
						List<String[]> list=new ArrayList<>();
						list.add(s);
						map.put(name, list);
					}else{
						map.get(name).add(s);
					}
				}
			}else if("act".equals(actOrArc)){
				if (b.getDistance0MomentsTime()>0&&"跳闸".equals(b.getActionType())) {
					String name=b.getBreakerName()+"-"+b.getPhase();
					String[] s={b.getFaultTime(),String.valueOf((float)b.getDistance0MomentsTime()/1000)};
					if(map.get(name)==null){
						List<String[]> list=new ArrayList<>();
						list.add(s);
						map.put(name, list);
					}else{
						map.get(name).add(s);
					}
				}
			}
			
		}
		return JSONObject.parseObject(JSON.toJSONString(map)).toString();
	}
	/**
	 * 获取断路器动作信息(熄弧时间)
	 * 
	 * @return
	 */

	@GetMapping("/getBreakerActInfosForArc/{option}")
	public String getBreakerActInfosForArc(@PathVariable("option") String option) {
		JSONObject jso = JSONObject.parseObject(option);
		List<BreakerActionInfoDTO> list = baqm.getBreakerActionInfoByOption(
				(String) jso.get("vol"),
				(String) jso.get("manufacturer"),
				(String) jso.get("type"), (String) jso.get("startDate"),
				(String) jso.get("endDate"));
		List<BreakerActionInfoDTO> result=new ArrayList<>();
		for(BreakerActionInfoDTO b:list){
			if(b.getXhTimeInterval()>0){
				result.add(b);
			}
		}
		return JSONArray.fromObject(result).toString();
	}


}
